package it.controllocredito.frontend.activities;

import it.controllocredito.R;
import it.controllocredito.frontend.fragments.params.PhoneParamsTabFragment;
import it.controllocredito.frontend.util.TabManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;

public class ParamsFragmentActivity extends SherlockFragmentActivity
{
	private TabHost mTabHost;
	private TabManager mTabManager;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.params_activity);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();

        mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);
        
        // Tab for Phone
        TabSpec phonespec = mTabHost.newTabSpec("Phone");
        // setting Title and Icon for the Tab
        phonespec.setIndicator("",getResources().getDrawable(R.drawable.icon_phone_tab));
        
        mTabManager.addTab(phonespec,PhoneParamsTabFragment.class,null);
        mTabManager.addTab(mTabHost.newTabSpec("prova2").setIndicator("Prova2"),PhoneParamsTabFragment.class,null);

        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
	}

	// quando viene chiusa l'activity
	@Override
	protected void onPause()
	{
		super.onPause();
		overridePendingTransition(R.anim.no_anim, R.anim.slide_left);
	}

	// al click del tasto "back"
	@Override
	public void onBackPressed()
	{
		super.onBackPressed();
		overridePendingTransition(R.anim.no_anim, R.anim.slide_left);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			finish();
			overridePendingTransition(R.anim.no_anim, R.anim.slide_left);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public boolean onOptionItemSelected(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			finish();
			overridePendingTransition(R.anim.no_anim, R.anim.slide_left);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case android.R.id.home:
	            // app icon in action bar clicked; go home
	            Intent intent = new Intent(this, MainFragmentActivity.class);
	            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(intent);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tab", mTabHost.getCurrentTabTag());
    }
	
}